﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using Models;

namespace BLL
{
    public class BaojiaManager
    {
        private BaojiaService objBaojiaService = new BaojiaService();
        /// <summary>
        /// 判断报价表中是否有对应行
        /// 有：更新数据 没有：新增数据
        /// </summary>
        /// <param name="baojia"></param>
        /// <returns></returns>
        public void InsertBaojia(Baojia baojia)
        {
            int count = objBaojiaService.GetBaojia(baojia.WorkParentId);
            if (count == 1)
            {
                objBaojiaService.UpdateBaojia(baojia);
            }
            else
            {
                objBaojiaService.InsertBaojia(baojia);
            }
        }
        /// <summary>
        /// 获取报价信息
        /// </summary>
        /// <param name="workParentId"></param>
        /// <returns></returns>
        public Baojia GetBaojiaInfo(int workParentId)
        {
            return objBaojiaService.GetBaojiaInfo(workParentId);
        }
        /// <summary>
        /// 获取最大Id行的Number值,根据规则生成新Number值
        /// </summary>
        /// <returns></returns>
        public string GetNewNumber()
        {
            string result = "";
            string number = objBaojiaService.GetLastNumber();
            int year = Convert.ToInt32(number.Substring(1, 2));
            int month = Convert.ToInt32(number.Substring(3, 2));
            int log = Convert.ToInt32(number.Substring(5, 3));
            if (year == Convert.ToInt32(Convert.ToString(DateTime.Now.Year).Substring(2, 2)))
            {

                if (month == Convert.ToInt32(DateTime.Now.Month))
                {
                    log = log + 1;
                    result = "V" + Convert.ToString(year) + Convert.ToString(month).PadLeft(2, '0') + log.ToString().PadLeft(3, '0');
                }
                else
                {
                    result = "V" + Convert.ToString(year) + Convert.ToString(month + 1).PadLeft(2, '0') + "001";
                }
            }
            else
            {
                result = "V" + Convert.ToString(year + 1) + "01" + "001";
            }
            return result;
        }
        /// <summary>
        /// 新增一条报价Log日志
        /// </summary>
        /// <param name="baojia"></param>
        /// <returns></returns>
        public int InsertBaojiaLog(Baojia baojia)
        {
            return objBaojiaService.InsertBaojiaLog(baojia);
        }
        /// <summary>
        /// 判断Id号获取是否成功
        /// </summary>
        /// <param name="no1"></param>
        /// <param name="no2"></param>
        /// <returns></returns>
        public string GetId(string no1, string no2)
        {
            int startId = objBaojiaService.GetId(no1);
            int endId = objBaojiaService.GetId(no2);
            string result = "";
            if (startId == 0)
            {
                result = "没有找到起始报价单号";
            }

            else if (endId == 0)
            {
                if (no2 == ""){ result = result = startId + "," + "-1"; }
                else { result = "没有找到截止报价单号"; }
            }
            else
            {
                result = startId + "," + endId;
            }
            return result;
        }
        public List<Baojia> GetBaojiaInfo(int startId, int endId, string customer)
        {
            return objBaojiaService.GetBaojiaInfo(startId, endId, customer);
        }
    }
}
